package com.mbigger.weather.service;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.List;

import org.springframework.stereotype.Service;

import com.mbigger.weather.util.XmlBuilder;
import com.mbigger.weather.vo.City;
import com.mbigger.weather.vo.CityList;

/**
 * City Data Service
 * 
 * @author BenWen
 */
@Service
public class CityDataServiceImpl implements CityDataService {

	@Override
	public List<City> listCity() throws Exception {
		// 读取XML文件
		//Resource resource = new ClassPathResource("citylist.xml");
		InputStream is = this.getClass().getClassLoader().getResourceAsStream("citylist.xml");
		BufferedReader br = new BufferedReader(
				new InputStreamReader(is, "utf-8"));
		StringBuffer buffer = new StringBuffer();
		String line = "";
		
		while((line = br.readLine()) != null) {
			buffer.append(line);
		}
		br.close();
		// XML转为Java对象
		CityList cityList = (CityList)XmlBuilder.xmlStrToObject(CityList.class, buffer.toString());
		return cityList.getCityList();
	}

}
